<template>
  <div class="header">
    <div class="page-title">标签管理</div>
    <el-button
        class="m-t-15"
        type="primary"
        size="small"
        icon="el-icon-edit"
        @click="handleCCreate"
    >添加标签</el-button
    >
    <el-table :data="tableData" class="m-t-5">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="id" width="100"></el-table-column>
      <el-table-column prop="manager" label="商户名称"></el-table-column>
      <el-table-column prop="store_name" label="门店名称" ></el-table-column>
      <el-table-column prop="content" label="内容" ></el-table-column>
      <el-table-column prop="font_color" label="字体颜色" ></el-table-column>
      <el-table-column prop="bg_color" label="背景色" ></el-table-column>
      <el-table-column prop="reorder" label="序号" ></el-table-column>
      <el-table-column label="操作" min-width="200">
        <template #default="v">
          <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              class="blue-btn"
              @click="handleEdit(v.row)"
          >编辑
          </el-button>
          <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="handleDelete(v.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        background
        hide-on-single-page
        layout="prev, pager, next"
        :total="total"
        :current-page="current"
        @current-change="changePage"
    >
    </el-pagination>
  </div>

  <el-dialog title="标签操作" v-model="dialogFormVisible" width="50%">
    <el-form>
      <el-form-item label="标签内容">
        <el-input
            v-model="form.content"
            autocomplete="off"
            placeholder="请输入名称"
        ></el-input>
      </el-form-item>

      <el-form-item label="门店">
        <el-select v-model="form.store_id" placeholder="请选择" style="margin-left:10px">
        <el-option
            v-for="item in packages"
            :key="item.id"
            :label="item.name"
            :value="item.id">
        </el-option>
      </el-select>
      </el-form-item>
      <el-form-item label="字体颜色">
        <el-color-picker v-model="form.font_color"></el-color-picker>
      </el-form-item>
      <el-form-item label="背景色">
        <el-color-picker v-model="form.bg_color"></el-color-picker>
      </el-form-item>
      <el-form-item label="排序">
        <el-input
            v-model="form.reorder"
            autocomplete="off"
            placeholder="请输入排序"
        ></el-input>
      </el-form-item>
    </el-form>

    <template v-slot:footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmitForm">确 定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>

import { UPLOAD_URL,CONST_DMOMAIN_URL } from "@/config/env";
import { request_tag_list, request_tag_store, request_tag_create, request_tag_update, request_tag_delete } from "@/util/api";

export default {
  name: "SeekType",
  data() {
    return {
      current: 1,
      total: 0,
      form: {},
      // 门店是否显示
      dialogFormVisible: false,
      uploadUrl: UPLOAD_URL,
      actionType: 0, // 1 添加  2 编辑
      packages:[],
    };
  },
  computed: {},
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.$nextTick(() => {
        request_tag_list()
            .then((res) => {
              const result = res.data;
              if (result.code == 200) {
                this.tableData = result.data.data;
                this.total = result.data.total;
                this.current = result.data.current;
              }
            })
            .catch();

        request_tag_store()
            .then((res) => {
              const result = res.data;
              if (result.code == 200) {
                this.packages = result.data.data;
              }
            })
      });
    },
    removeBanner(key){
      this.$confirm("确定要删除此图片吗？", "系统提示", {
        type: "warning",
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
          .then(() => {
            this.form.banners.splice(key, 1)
          })

    },
    handleStation(row) {
      this.$router.push('/station?id=' + row.id)
    },

    handleUploadSuccessByBanners(res,raw, key) {
      this.form.banners[key] = CONST_DMOMAIN_URL + res.data;
      console.log(this.form.banners)
    },

    // 添加
    handleCCreate() {
      this.actionType = 1;
      this.dialogFormVisible = true;
    },
    // 编辑
    handleEdit(row) {
      this.actionType = 2;
      this.form = row;

      this.dialogFormVisible = true;
    },
    // 提交
    handleSubmitForm() {
      delete this.form.manager;
      delete this.form.store_name;
      let apiFunc;
      switch (this.actionType) {
        case 1: // 添加
          apiFunc = request_tag_create;
          break;
        case 2: // 编辑
          apiFunc = request_tag_update;
          break;
        default:
          this.$message.error({
            message: "操作类型不正确",
            offset: 70,
          });
          return false;
      }
      apiFunc({
        ...this.form,
      }).then((res) => {
        const result = res.data;
        if (result.code == 200) {
          this.dialogFormVisible = false;
          this.$notify.success({
            title: "系统提示",
            message: result.message,
            offset: 70,
          });
          location.reload();
        } else {
          this.$message.error({
            message: result.message,
            offset: 70,
          });
        }
      });
    },
    // 删除
    handleDelete(row) {
      this.$confirm("此操作将永久删除，是否继续？", "系统提示", {
        type: "warning",
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
          .then(() => {
            request_tag_delete({
              id: row.id,
            })
                .then((res) => {
                  const result = res.data;
                  if (result.code == 200) {
                    this.$message.success({
                      message: "删除成功！",
                      offset: 70,
                    });
                    this.changePage(this.page)

                  } else {
                    this.$message.error({
                      message: result.message,
                      offset: 70,
                    });
                  }
                })
                .catch();
          })
          .catch(() => {
            this.$message.info({
              message: "已取消删除！",
              offset: 70,
            });
          });
    },
    // 分页
    changePage(page) {
      request_tag_list({
        page,
      })
          .then((res) => {
            const result = res.data;
            if (result.code == 200) {
              this.tableData = result.data.data;
              this.total = result.data.total;
              this.current = result.data.current;
            }
          })
          .catch();
    },
  },
};
</script>

<style scoped  lang="less">
.avatar-uploader {
  float:left;
  margin-right:10px;
  margin-bottom:10px;
}
.banner-add {
  clear:both;
  margin-top:20px;
}
</style>